/**
 * Carnegie Mellon University
 * School of Computer Science
 * MSIT eBusiness Technology
 * 
 * Team 8 - Infinity (2011 Spring)
 * infinity-team8@googlegroups.com
 * 
 * Professor: Jeffrey L. Eppinger
 * Program Faculty: Sujata Telang
 */

package controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Model;
import model.CustomerDAO;
import model.PositionDAO;

import org.mybeans.dao.DAOException;

import databean.CustomerBean;
import databean.PositionBean;


public class CustomerViewAccountAction extends Action {

	private CustomerDAO		customerDAO;
	private PositionDAO		positionDAO;

	public CustomerViewAccountAction(Model model) {
    	customerDAO		= model.getCustomerDAO();
    	positionDAO		= model.getPositionDAO();
	}

	public String getName() { return "customer-view-account.do"; }

	public String perform(HttpServletRequest request,HttpServletResponse response) {
        // Set up the errors list
        List<String> errors = new ArrayList<String>();
        request.setAttribute("errors",errors);
        
		try {
            // Set up user list for nav bar
			request.setAttribute("customerList",customerDAO.getCustomers());

			CustomerBean customer = (CustomerBean) request.getSession(false).getAttribute("customer");
			request.getSession().setAttribute("customer",customer);
			
			PositionBean[] positions = positionDAO.lookup(customer.getCustomerID());
			request.setAttribute("positions",positions);
			
			
	        return "customer-view-account.jsp";
        } catch (DAOException e) {
        	errors.add(e.getMessage());
        	return "customer-error.jsp";
        }
    }
}
